<template>
  <div class="app-container">
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <!-- <el-card style="margin-top:-10px;">
        <div slot="header" class="clearfix">
          <span style="color:#329dff;font-weight:bold;font-size:18px;">新建子工程</span>
        </div> -->

        <el-card style="margin-top:-2px;">
          <div slot="header" class="clearfix">
            <span style="color:#329dff;font-weight:bold;font-size:14px;">子工程信息</span>
          </div>

          <el-row>
              <el-col :span="12">
                <el-form-item label="子工程编码" prop="subEngineerNo">
                  <el-input v-model="form.subEngineerNo" disabled />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="子工程代码" prop="code">
                  <el-input v-model="form.code" />
                </el-form-item>
              </el-col>
          </el-row>

          <el-row>
              <el-col :span="12">
                <el-form-item label="子工程名称" prop="name">
                  <el-input v-model="form.name" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="子工程金额" prop="costConstruction">
                  <el-input-number style="width:95%;color:red;" v-model="form.costConstruction" :controls="false" :precision="4" /><span style="color:blue;margin-left:5px;">万元</span>
                </el-form-item>
              </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="子工程类型" prop="engineerType">
                <el-select v-model="form.engineerType" placeholder="请选择子工程类型" style="width:100%;">
                  <el-option v-for="dict in dict.type.sys_sub_engineer_type" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="所属工程" prop="engineerId">
                <el-select v-model="form.engineerId" filterable clearable placeholder="请选择所属工程" style="width:100%;" @change="changeEngineer">
                  <el-option v-for="item in engineerList" :key="item.uuid" :label="item.name" :value="item.uuid"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="启动日期" prop="startTime">
                <el-date-picker clearable size="small" style="width:100%;" v-model="form.startTime" type="date" value-format="yyyy-MM-dd" placeholder="选择启动日期"></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="竣工日期" prop="endTime">
                <el-date-picker clearable size="small" style="width:100%;" v-model="form.endTime" type="date" value-format="yyyy-MM-dd" placeholder="选择竣工日期"></el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="负责人" prop="chargerId">
                <el-select v-model="form.chargerId" filterable placeholder="请选择负责人" style="width:100%;">
                  <el-option v-for="item in participants" :key="item.userId" :label="item.nickName" :value="item.userId"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="负责部门" prop="deptId">
                <treeselect v-model="form.deptId" :options="deptOptions" :show-count="true" placeholder="请选择负责部门" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
            <el-form-item label="施工单位" prop="ownerDept">
              <el-input v-model="form.ownerDept" />
            </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="监理单位" prop="supervisor">
                <el-input v-model="form.supervisor" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
            <el-form-item label="施工联系人" prop="contactPerson">
              <el-input v-model="form.contactPerson" />
            </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="联系方式" prop="contactPhone">
                <el-input v-model="form.contactPhone" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="施工地点" prop="constructSite">
                <el-input v-model="form.constructSite" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="所属项目" prop="projectName">
                <el-input v-model="form.projectName" disabled />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="24">
              <el-form-item label="子工程描述" prop="describe">
                <el-input type="textarea" v-model="form.describe" :rows="6"></el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="24">
              <el-form-item label="补充说明" prop="remark">
                <el-input type="textarea" v-model="form.remark" :rows="6"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-card>

        <el-card style="margin-top:10px;">
          <div slot="header" class="clearfix">
            <span style="color:#329dff;font-weight:bold;font-size:14px;">子工程附件</span>
          </div>
          <el-row>
            <el-col :span="24">
              <el-form-item prop="contractAttach">
                <image-upload-custom v-model="form.contractAttach" :limit="10000" :imageName="imageName"></image-upload-custom>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item prop="materialAttach">
                <file-upload-custom
                  :fileSize="10000"
                  :attachName="attachOtherName"
                  :fileList_show="fileList_show"
                  v-model="form.materialAttach"
                  @addlist="fileList_show_add"
                  @deletlist="fileList_show_delet"
                />
              </el-form-item>
            </el-col>
          </el-row>
        </el-card>

      <!-- </el-card> -->
    </el-form>
    <center>
      <div slot="footer" class="dialog-footer" style="margin-top:20px;">
        <el-button type="primary" @click="submitForm">保 存</el-button>
        <el-button @click="cancel">重 置</el-button>
      </div>
    </center>
  </div>
</template>

<script>
import { treeselect } from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { listAllUser } from "@/api/system/user";
import { createSubEngineerNo,editSubEngineer } from "@/api/system/subengineer";
import { getEngineerList,getProjectInfo4SubEngineer } from "@/api/system/engineer";

export default {
  name: "NewSubEngineer",
  dicts: ['sys_sub_engineer_type'],
  components:{ Treeselect },
  data() {
    return {
      attachPdfName: '上传PDF',
      attachOtherName: '上传文件',
      imageName: '上传图片',
      fileList_show:[],
      participants: [],
      deptOptions: [],
      engineerList: [],
      form: {},
      rules: {

      },
    };
  },
  created() {
    this.createSubEngineerNo();
    this.listAllUser();
    this.getTreeselect();
    this.getEngineerList();
  },
  methods: {
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.uuid != null) {
            editSubEngineer(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.$router.push({path:"/subengineer/querysubengineer"});
              this.reset();
            });
          } else {
            editSubEngineer(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.$router.push({path:"/subengineer/querysubengineer"});
              this.reset();
            });
          }
        }
      });
    },
    changeEngineer(val){
      if(val!=null&&val.length>0){
        let query = {
          uuid:val
        }
        getProjectInfo4SubEngineer(query).then(res =>{
          this.form.projectId = res.data.projectId;
          this.form.projectName = res.data.projectName;
        })
      }else{
        this.form.projectId = null;
        this.form.projectName = null;
      }
    },
    getEngineerList(){
      getEngineerList().then(res=>{
        this.engineerList = res.data;
      })
    },
    // 取消按钮
    cancel() {
      this.createSubEngineerNo();
    },
    createSubEngineerNo(){
      createSubEngineerNo().then(res =>{
        this.reset();
        this.form.subEngineerNo = res.data.subEngineerNo;
      })
    },
    // 表单重置
    reset() {
      this.form = {
        uuid: null,
        subEngineerNo: null,
        name: null,
        projectId: null,
        projectName: null,
        engineerId: null,
        code: null,
        startTime: null,
        endTime: null,
        describe: null,
        engineerType: null,
        status: null,
        chargerId: null,
        deptId: null,
        contactPerson: null,
        contactPhone: null,
        costConstruction: null,
        ownerDept: null,
        ownerContact: null,
        supervisor: null,
        constructSite: null,
        contractAttach: null,
        materialAttach: null,
        roleIds: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
      };
      this.resetForm("form");
    },
    listAllUser(){
      listAllUser().then(res =>{
        this.participants = res.data;
      })
    },
    getTreeselect() {
      treeselect().then(response => {
        this.deptOptions = response.data;
      });
    },
    // 文件上传接收子组件值，同步（增）
    fileList_show_add(val){
      this.fileList_show = this.fileList_show.concat(val);
    },
    // 文件上传接收子组件值，同步（减）
    fileList_show_delet(val){
      this.fileList_show.splice(val, 1);
    },
  }
};
</script>
